﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Matrix.Common.Utility
{
    public static class StringExtension
    {
        static int _rep = 0;
        /// <summary> 
        /// 生成随机数字字符串 
        /// </summary> 
        /// <param name="codeCount">待生成的位数</param> 
        /// <returns>生成的数字字符串</returns> 
        public static string GenerateCheckCodeNum(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + _rep;
            _rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> _rep)));
            for (int i = 0; i < codeCount; i++)
            {
                int num = random.Next();
                str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
            }
            return str;
        }
        // <summary>
        /// 需要将字符串截断到合适的长度
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len">字符串长度，以汉字个数计算，英文算半个</param>
        /// <returns></returns>
        public static string GetSubString(string str, int length)
        {

            string temp = str;

            int j = 0;

            int k = 0;

            for (int i = 0; i < temp.Length; i++)
            {

                if (Regex.IsMatch(temp.Substring(i, 1), @"[\u4e00-\u9fa5]+"))
                {

                    j += 2;

                }

                else
                {

                    j += 1;

                }

                if (j <= length)
                {

                    k += 1;

                }

                if (j >= length)
                {

                    return temp.Substring(0, k);

                }

            }

            return temp;

        }

        /// <summary>
        /// 微信商户订单号生成
        /// </summary>
        /// <returns></returns>
        public static string TimerRandomNumber(string mchId)
        {
            string num = "0123456789";
            string randomStr = RandomStr(num, 10);
            return mchId + System.DateTime.Now.ToString("yyyyMMdd") + randomStr;
        }
        public static string RandomStr(string str, int Length)
        {
            string result = string.Empty;
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                result += str[rd.Next(str.Length)];
            }
            return result;
        }
        #region 去掉字符串前后字符
        public static string StrSubString(string str)
        {

            if (str != null && str != "")
            {

                str = str.Substring(0, str.Length - 1);
                str = str.Substring(1, str.Length - 1);
            }
            return str;
        }
        #endregion
        /// <summary>
        /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.
        /// </summary>
        /// <param name="startTime">第一个日期和时间</param>
        /// <param name="endTime">第二个日期和时间</param>
        /// <returns></returns>
        public static int DateDiff(DateTime startTime, DateTime endTime)
        {
            int dateDiff = 0;
            try
            {
                TimeSpan ts1 = new TimeSpan(startTime.Ticks);
                TimeSpan ts2 = new TimeSpan(endTime.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                dateDiff = ts.Hours;

            }
            catch
            {

            }
            return dateDiff;
        }

        /// <summary>
        /// 生成随机验证码,,key
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string GetCode(int num)
        {
            //string[] source ={"0","1","2","3","4","5","6","7","8","9",
            //             "A","B","C","D","E","F","G","H","I","J","K","L","M","N",
            //           "O","P","Q","R","S","T","U","V","W","X","Y","Z"};
            string[] source ={"0","1","2","3","4","5","6","7","8","9"
                     };
            string code = "";
            Random rd = new Random();
            for (int i = 0; i < num; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }

        public static string ToBase64String(this string data)
        {
            if (!string.IsNullOrWhiteSpace(data))
            {
                string base64Str = Convert.ToBase64String(Encoding.UTF8.GetBytes(data));
                return base64Str.Replace('+', '-').Replace('/', '_');
            }
            else
            {
                return null;
            }
        }
        public static string FromBase64String(this string base64string)
        {
            if (!string.IsNullOrWhiteSpace(base64string))
            {
                var data = Convert.FromBase64String(base64string.Replace('-', '+').Replace('_', '/'));
                return Encoding.UTF8.GetString(data);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 判断字符串是否为base64字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsBase64String(this string str)
        {
            return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
        }
        private static int rep = 0;
        /// <summary> 
        /// 生成随机字母字符串(数字字母混和) 
        /// </summary> 
        /// <param name="codeCount">待生成的位数</param> 
        /// <returns>生成的字母字符串</returns> 
        public static string GenerateCheckCode(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codeCount; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;
        }
    }
}
